<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 12.3.&nbsp; Displaying Results with Embedded Links</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-12-SECT-3"></a>
<h3 id="title-IDANSD1L" class="docSection1Title">12.3. Displaying Results with Embedded Links</h3>
<p class="docText">You may want to give your web user the ability to click on a hyperlink to launch an action that relates to the current row in the results from a query. You do this by adding URL links to the results of a query when they display on the screen. The links contain a unique identifier to the row and the script that handles the action.</p>
<p class="docText">The PHP script that is the target of the link typically queries the database based on the unique identifier that was passed to it. The types of action you can do range fromadding or deleting a row to expanding on details from a related table, such as authors for book titles.</p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-12-EX-4">Example 12-4</a>, let's display the list of titles with hyperlinks to purchase the titles.</p>
<a name="learnphpmysql-CHP-12-EX-4"></a><h5 id="title-IDA5SD1L" class="docExampleTitle">Example 12-4. Using embedded links to provide a purchase button in pear_purchase_example.php</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
}
$query = "SELECT * FROM `books`";
$result = $connection-&gt;query($query);
if (DB::isError($result)){
die("Could not query the database: &lt;br /&gt;". $query." ".DB::errorMessage($result));
}
echo '&lt;table border="1"&gt;';
echo "&lt;tr&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Pages&lt;/th&gt;&lt;th&gt;Buy&lt;/th&gt;&lt;/tr&gt;";
while ($result_row = $result-&gt;fetchRow(DB_FETCHMODE_ASSOC)) {
echo "&lt;tr&gt;&lt;td&gt;";
echo $result_row["title"] . '&lt;/td&gt;&lt;td&gt;';
echo $result_row["pages"] . '&lt;/td&gt;&lt;td&gt;';
echo '&lt;a href="purchase.php?title_id='.$result_row["title_id"].'"&gt;Click
to purchase&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;';
}
echo "&lt;/table&gt;";
$connection-&gt;disconnect();
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-12-EX-4">Example 12-4</a>, you modify the format of the last bolded table cell to build a hyperlink for purchasing the book. The target of that link is the file <span class="docEmphasis">purchase.php</span>, which is defined in <a class="docLink" href="#learnphpmysql-CHP-12-EX-5">Example 12-5</a>. You send it a parameter called <tt>title_id</tt>, which is the primary key from the <tt>titles</tt> table. This unique ID specifies which book the user wants to purchase, and is used as a link in the table shown in <a class="docLink" href="#learnphpmysql-CHP-12-FIG-4">Figure 12-4</a>.</p>
<a name="learnphpmysql-CHP-12-FIG-4"></a><P><center>
<H5 class="docFigureTitle">Figure 12-4. Users can click the purchase link to add the purchase to the purchases table</h5>
<img border="0" alt="" width="549" height="149" SRC="images/learnphpmysql_1204.jpg">
</center></P><br>
<p class="docText">Next, you'll define the script that handles the purchase action in <a class="docLink" href="#learnphpmysql-CHP-12-EX-5">Example 12-5</a>.</p>

<a name="learnphpmysql-CHP-12-EX-5"></a><h5 id="title-IDAWUD1L" class="docExampleTitle">Example 12-5. The file purchase.php processes the user action based on the title_id parameter</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
1 &lt;?php
2 require_once('db_login.php');
3 require_once('DB.php');
4 $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
5 if (DB::isError($connection)){
6 die ("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
7 }
8 $title_id = $_GET["title_id"];
9 $user_id = 'mdavis';
10 $query = "INSERT INTO `purchases` VALUES (NULL,'$user_id',$title_id,NULL)";
11 $result = $connection-&gt;query($query);
12 if (DB::isError($result)){
13 die("Could not query the database: &lt;br /&gt;". $query." ".DB::errorMessage($result));
14 }
15 ?&gt;
16 &lt;html&gt;
17 &lt;head&gt;
18 &lt;title&gt;Thanks for your purchase!&lt;/title&gt;
19 &lt;meta http-equiv="refresh" content="4; url=pear_purchase_example.php"&gt;
20 &lt;/head&gt;
21 &lt;body&gt;
22 Thanks for your purchase!&lt;br /&gt;
23 &lt;?php
24
25 $query = "SELECT * FROM purchases NATURAL JOIN books NATURAL JOIN authors";
26 $result = $connection-&gt;query($query);
27 if (DB::isError($result)){
28 die("Could not query the database: &lt;br /&gt;". $query." ".DB::errorMessage($result));
29 }
30 echo '&lt;table border="1"&gt;';
31 echo "&lt;tr&gt;&lt;th&gt;User&lt;/th&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Pages&lt;/th&gt;";
32 echo "&lt;th&gt;Author&lt;/th&gt;&lt;th&gt;Purchased&lt;/th&gt;&lt;/tr&gt;";
33 while ($result_row = $result-&gt;fetchRow(DB_FETCHMODE_ASSOC)) {
34 echo "&lt;tr&gt;&lt;td&gt;";
35 echo $result_row["user_id"] . '&lt;/td&gt;&lt;td&gt;';
36 echo $result_row["title"] . '&lt;/td&gt;&lt;td&gt;';
37 echo $result_row["pages"] . '&lt;/td&gt;&lt;td&gt;';
38 echo $result_row["author"] . "&lt;/td&gt;&lt;td&gt;";
39 echo $result_row["purchased"] . "&lt;/td&gt;&lt;/tr&gt;";
40 }
41 echo "&lt;/table&gt;";
42
43 $connection-&gt;disconnect();
44 ?&gt;
45 &lt;/body&gt;
46 &lt;/html&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText">Since this example is fairly lengthy, we'll discuss the major additions on a line-by-line basis.</P>
<ul><li><p class="docList">Line 8 takes the parameter from the calling script and assigns it to a local variable called <tt>$title_id</tt>, which we'll reference in the <tt>insert</tt> statement.</P></LI><li><p class="docList">Line 9 sets a <tt>$user_id</tt> variable to <tt>mdavis</tt>. Ideally, the username wouldn't be hardcoded. In the next chapter, you'll learn about logging users into a session that holds their identity.</p></li><li><p class="docList">Line 10 sets up the query with the <tt>INSERT</tt> statement using the user-supplied values.</P></li><LI><p class="docList">Line 19 uses a META tag to redirect users back to the page from which they came after briefly displaying a message that their purchases (that you processed as an <tt>INSERT</tt> to the database) were successful. The syntax for redirecting to another page after a delay is:</p><pre>
&lt;meta http-equiv="refresh" content="<tt><i>seconds_before_refreshing</I></tt>; url=<tt><I>url_to_redirect_to</i></tt>"&gt;
</pre><BR>
</li><li><p class="docList">The <tt>META</tt> statement should be placed in the <tt>&lt;head&gt;</tt> section of the HTML.</p></li><li><p class="docList">Line 25 defines a new query to select all purchases. Subsequent lines display the results in an HTML table.</p></li></ul>
<p class="docText">The end result is that a new purchase is added to the <tt>purchases</tt> table, and the user briefly sees the contents of the purchases table before returning to the previous page.</p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-12-FIG-5">Figure 12-5</a> shows the purchase record we created in <a class="docLink" href="learnphpmysql-CHP-12-SECT-2.html#learnphpmysql-CHP-12-EX-3">Example 12-3</a>, plus the newly created entry from <a class="docLink" href="#learnphpmysql-CHP-12-EX-4">Example 12-4</a>.</p>
<a name="learnphpmysql-CHP-12-FIG-5"></a><p><center>
<h5 class="docFigureTitle">Figure 12-5. After clicking "Click to purchase" for "Linux in a Nutshell"</H5>
<img border="0" alt="" width="549" height="186" SRC="images/learnphpmysql_1205.jpg">
</center></p><br>
<p class="docText">With the click of a link, you can add customized data to your table. Let's integrate form submission and insert data.</P>

<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-2.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-4.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
